from django.db import models

from DjangoUeditor.models import UEditorField
from apps.users.models import BaseModel, UserProfile
from datetime import datetime

# Create your models here.
category_chioes = (('pxjg', "培训机构"), ('gx', '高校'), ('gr', '个人'))


# 城市
class City(BaseModel):
    name = models.CharField(verbose_name="城市名", max_length=20)
    desc = models.CharField(max_length=200, verbose_name="描述")

    class Meta:
        verbose_name = "城市"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


# 授课机构 1对多 城市
class CourseOrg(BaseModel):
    city = models.ForeignKey(City, on_delete=models.CASCADE, verbose_name="城市")
    name = models.CharField(max_length=50, verbose_name="机构名称")
    # desc = models.TextField(verbose_name="描述")
    desc = UEditorField(verbose_name="描述", width=950, height=500, imagePath="courses/ueditor/images/",
                        filePath="courses/ueditor/files/", default="")
    tag = models.CharField(verbose_name="机构标签", default="全国知名", max_length=10)
    category = models.CharField(verbose_name="机构类别", choices=category_chioes, default="pxjg", max_length=4)
    fav_nums = models.IntegerField(verbose_name="收藏人数", default=0)
    click_nums = models.IntegerField(verbose_name="点击数", default=0)
    students = models.IntegerField(verbose_name="学习人数", default=0)
    courses_num = models.IntegerField(verbose_name="课程数", default=0)
    address = models.CharField(verbose_name="地址", max_length=150)
    image = models.ImageField(upload_to="org/%Y/%m", verbose_name="logo", max_length=100)

    is_auth = models.BooleanField(default=False, verbose_name="是否认证")
    is_gold = models.BooleanField(default=False, verbose_name="是否金牌")

    class Meta:
        verbose_name = "课程机构"
        verbose_name_plural = verbose_name

    def courses(self):
        courses = self.courses_set.filter(is_classics=True)[:3]
        return courses

    def course_nums(self):
        return self.courses_set.all().count()

    def teacher_nums(self):
        return self.teacher_set.all().count()

    def __str__(self):
        return self.name


# 机构教师 多对1 授课机构
class Teacher(models.Model):
    user = models.OneToOneField(UserProfile, on_delete=models.SET_NULL, null=True, blank=True, verbose_name="用户")
    org = models.ForeignKey(CourseOrg, on_delete=models.CASCADE, verbose_name=u"所属机构")
    name = models.CharField(max_length=50, verbose_name=u"教师名")
    work_years = models.IntegerField(default=0, verbose_name=u"工作年限")
    work_company = models.CharField(max_length=50, verbose_name=u"就职公司")
    work_position = models.CharField(max_length=50, verbose_name=u"公司职位")
    points = models.CharField(max_length=50, verbose_name=u"教学特点")
    click_nums = models.IntegerField(default=0, verbose_name=u"点击数")
    fav_nums = models.IntegerField(default=0, verbose_name=u"收藏数")
    age = models.IntegerField(default=18, verbose_name=u"年龄")
    image = models.ImageField(default='', upload_to="teacher/%Y/%m", verbose_name=u"头像", max_length=100)
    add_time = models.DateTimeField(default=datetime.now)

    class Meta:
        verbose_name = u"教师"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name

    def teacher_course(self):
        return self.courses_set.all()

    def course_nums(self):
        return self.courses_set.all().count()
